package com.zhb.cloud.service.plant.active.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhb.cloud.core.utils.PrizeUtils;
import com.zhb.cloud.distribute.lock.DistributedLock;
import com.zhb.cloud.distribute.lock.LockException;
import com.zhb.cloud.distribute.lock.LockPath;
import com.zhb.cloud.distribute.lock.LockFactory;
import com.zhb.cloud.plant.active.mapper.PrizeMapper;
import com.zhb.cloud.plant.active.mapper.UserMapper;
import com.zhb.cloud.plant.active.mapper.UserPrizeMapper;
import com.zhb.cloud.plant.active.model.Prize;
import com.zhb.cloud.plant.active.model.User;
import com.zhb.cloud.plant.active.model.UserPrize;
import com.zhb.cloud.plant.active.service.UserService;
import com.zhb.cloud.utils.UUIDUtil;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PrizeMapper prizeMapper;
	@Autowired
	private UserPrizeMapper userPrizeMapper;
	@Autowired
	private LockFactory lockFactory;

	/**
	 * 抽奖的具体实现--zk实分布式锁
	 */
	@Override
	public Prize lottery(String userId) {
		logger.info(userId + "哈哈我开始抽奖了");
		// 1.查询奖品
		List<Prize> prizes = prizeMapper.findAllPrize();
		// 1.1计算获得奖品
		Prize prize = PrizeUtils.getAwardPrize(prizes);
		logger.info("中的奖品是" + prize);
		// 获得锁
		DistributedLock lock = lockFactory.obtainLock(LockPath.ACTIVITY_LOCK);
		boolean b = false;
		try {
			while (!b) {
				b = lock.tryLock(10, TimeUnit.SECONDS);
				if (b) {
					reducePrizeNumber(prize.getId()); // 更新奖品数量
					reduceAwardCount(userId);// 更新用户抽奖次数
					recordUserAward(userId, prize.getId());// 插入抽奖记录
				}
			}
		} catch (LockException e) {
			prize = null;
			e.printStackTrace();
		} finally {
			lock.unlock();// 释放锁
		}
		return prize;
	}

	/**
	 * @Description: 减少奖品数量
	 * @author: zhb zhb1316783262@163.com
	 * @date: 2017年11月20日 上午11:00:08
	 * @param prizeName
	 * @param userId
	 */
	public void reducePrizeNumber(String prizeId) {
		logger.info("进入方法reducePrizeNumber减少奖品的数量");
		Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
		prize.setPrizeAmount(prize.getPrizeAmount() - 1);
		prizeMapper.updateByPrimaryKeySelective(prize);
	}

	/**
	 * 更新用户剩余抽奖次数
	 */
	@Override
	public int reduceAwardCount(String userId) {
		logger.info("进入方法reduceAwardCount 减少用户的抽奖次数");
		User user = userMapper.selectByPrimaryKey(userId);
		user.setAwardCount(user.getAwardCount() - 1);
		return userMapper.updateByPrimaryKeySelective(user);
	}

	/**
	 * 插入用户的抽奖记录
	 */
	@Override
	public int recordUserAward(String userId, String prizeId) {
		logger.info("进入方法recordUserAward 记录抽奖记录");
		logger.info("进入方法recordUserAward 记录抽奖记录");
		UserPrize userPrize = new UserPrize();
		userPrize.setId(UUIDUtil.getId());
		userPrize.setPrizeId(prizeId);
		userPrize.setUserId(userId);
		userPrize.setStatus(1);
		userPrize.setCreateTime(new Date());
		return userPrizeMapper.insertSelective(userPrize);
	}

	@Override
	public User findById(String id) {
		return null;
	}

	@Override
	public int saveModel(User model) {
		return 0;
	}

	@Override
	public int updateModel(User model) {
		return 0;
	}

	@Override
	public int deleteModel(String id) {
		return 0;
	}

	@Override
	public long totalAmount(User model, Object... objects) {
		return 0;
	}

	@Override
	public int batchInsert(List<User> list) {
		return userMapper.batchInsert(list);
	}

	@Override
	public List<User> selectModelByCondition() {
		// TODO Auto-generated method stub
		return null;
	}

}
